package com.wc.codeforces.二分.Bomb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/7/28 12:11
 * @description https://codeforces.com/contest/1996/problem/F
 */
public class Main {
    /**
     * 思路：
     * 将a[i], a[i] - b[i], a[i] - 2b[i], 。。。 0排列在一起
     * 找到第 k 大的数, 用二分
     * a[i] - (n - 1) * b[i] >= x
     * n <= (a[i] - x]) / b[i] + 1 是多少项满足
     * 思路就明确了
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[] a = new int[N];
    static int[] b = new int[N];
    static int n, k;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            k = sc.nextInt();
            for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
            for (int i = 1; i <= n; i++) b[i] = sc.nextInt();
            long res = 0;
            long l = 0, r = (int) 1e9 + 10;
            while (l < r) {
                long mid = l + r + 1 >> 1;
                if (check(mid)) l = mid;
                else r = mid - 1;
            }
            int cnt = 0;
            for (int i = 1; i <= n; i++) {
                if (a[i] >= l) {
                    long N = (a[i] - l) / b[i] + 1;
                    res += (a[i] + a[i] - (N - 1) * b[i]) * N / 2;
                    cnt += N;
                }
            }
            if (cnt > k) res -= l * (cnt - k);
            out.println(res);
        }
        out.flush();
    }

    static boolean check(long mid) {
        long cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] >= mid)
                cnt += (a[i] - mid) / b[i] + 1;
        }
        return cnt >= k;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
